package com.ruoyi.other.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.other.domain.CityFareGlobalConfig;
import com.ruoyi.other.service.ICityFareGlobalConfigService;
import com.ruoyi.other.vo.SelectNightTimeVo;
import com.ruoyi.other.vo.UpdateBanchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 城市路费全局配置Controller
 *
 * @author ruoyi
 * @date 2025-04-16
 */
@RestController
@RequestMapping("/cityFareGlobalConfig/cityFareGlobalConfig")
public class CityFareGlobalConfigController extends BaseController {
    @Autowired
    private ICityFareGlobalConfigService cityFareGlobalConfigService;

    /**
     * 查询城市路费全局配置列表
     */
    @PreAuthorize("@ss.hasPermi('cityFareGlobalConfig:cityFareGlobalConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(CityFareGlobalConfig cityFareGlobalConfig) {
        startPage();
        List<CityFareGlobalConfig> list = cityFareGlobalConfigService.selectCityFareGlobalConfigList(cityFareGlobalConfig);
        return getDataTable(list);
    }

    /**
     * 导出城市路费全局配置列表
     */
    @PreAuthorize("@ss.hasPermi('cityFareGlobalConfig:cityFareGlobalConfig:export')")
    @Log(title = "城市路费全局配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CityFareGlobalConfig cityFareGlobalConfig) {
        List<CityFareGlobalConfig> list = cityFareGlobalConfigService.selectCityFareGlobalConfigList(cityFareGlobalConfig);
        ExcelUtil<CityFareGlobalConfig> util = new ExcelUtil<CityFareGlobalConfig>(CityFareGlobalConfig.class);
        util.exportExcel(response, list, "城市路费全局配置数据");
    }

    /**
     * 获取城市路费全局配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('cityFareGlobalConfig:cityFareGlobalConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cityFareGlobalConfigService.selectCityFareGlobalConfigById(id));
    }

    /**
     * 新增城市路费全局配置
     */
    @PreAuthorize("@ss.hasPermi('cityFareGlobalConfig:cityFareGlobalConfig:add')")
    @Log(title = "城市路费全局配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CityFareGlobalConfig cityFareGlobalConfig) {
        return toAjax(cityFareGlobalConfigService.insertCityFareGlobalConfig(cityFareGlobalConfig));
    }

    /**
     * 修改城市路费全局配置
     */
    @PreAuthorize("@ss.hasPermi('cityFareGlobalConfig:cityFareGlobalConfig:edit')")
    @Log(title = "城市路费全局配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CityFareGlobalConfig cityFareGlobalConfig) {
        return toAjax(cityFareGlobalConfigService.updateCityFareGlobalConfig(cityFareGlobalConfig));
    }

    /**
     * 删除城市路费全局配置
     */
    @PreAuthorize("@ss.hasPermi('cityFareGlobalConfig:cityFareGlobalConfig:remove')")
    @Log(title = "城市路费全局配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cityFareGlobalConfigService.deleteCityFareGlobalConfigByIds(ids));
    }

    /**
     * 批量修改
     *
     * @param
     * @return
     * @Date 2025 4月 18 15:12
     */
    @PreAuthorize("@ss.hasPermi('cityFareGlobalConfig:cityFareGlobalConfig:add')")
    @Log(title = "城市路费全局配置", businessType = BusinessType.INSERT)
    @PostMapping("updateBanch")
    public AjaxResult updateBanch(@RequestBody List<UpdateBanchVo> updateBanchVos) {
        return toAjax(cityFareGlobalConfigService.updateBanch(updateBanchVos));
    }

    @PreAuthorize("@ss.hasPermi('cityFareGlobalConfig:cityFareGlobalConfig:add')")
    @Log(title = "城市路费全局配置", businessType = BusinessType.INSERT)
    @PostMapping("selectNightTime")
    public AjaxResult selectNightTime(@RequestBody SelectNightTimeVo selectNightTimeVo) {
        return success(calculateRemainingTimeRange(selectNightTimeVo.getDayStartTime(),selectNightTimeVo.getDayEndTime()));
    }

    public static String calculateRemainingTimeRange(String dayStartTime,String dayEndTime) {
        // 解析输入时间范围
        LocalTime startTime = LocalTime.parse(dayStartTime, DateTimeFormatter.ofPattern("HH:mm"));
        LocalTime endTime = LocalTime.parse(dayEndTime, DateTimeFormatter.ofPattern("HH:mm"));
        // 计算剩余时间范围
        String remainingStart;
        String remainingEnd;

        if (dayStartTime.equals(dayEndTime)){
            remainingStart = "";
            remainingEnd = "";
        }else {
            remainingStart = endTime.plusMinutes(1).format(DateTimeFormatter.ofPattern("HH:mm"));
            remainingEnd = startTime.minusMinutes(1).format(DateTimeFormatter.ofPattern("HH:mm"));
        }
        // 格式化输出
        return remainingStart + "-" + remainingEnd;
    }

}
